package org.microdream.easy.deploy.service;

import ice.server.comm.BaseServiceImpl;
import ice.server.comm.CommRepository;
import ice.server.comm.ParamBulider;
import ice.tool.PageVO;
import ice.tool.ResultMap;
import org.microdream.easy.deploy.dao.DUserRepository;
import org.microdream.easy.deploy.po.DUser;
import org.microdream.easy.deploy.po.DUserOrg;
import org.microdream.easy.deploy.utils.DateUtil;
import org.microdream.easy.deploy.utils.ListUtil;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DUserService extends BaseServiceImpl<DUser, Integer>{

    @Resource
    private DUserRepository dUserRepository;

    @Resource
    private DUserOrgService dUserOrgService;

    @Override
    public CommRepository<DUser, Integer> getRepository() {

        return dUserRepository;
    }

    @Override
    public Integer getIdenty(DUser t) {
        return t.getId();
    }

    public Map searchByPage(Map gson){
        Map resultMap = new HashMap();
        PageVO<DUser> pv = new PageVO<>();
        Page<DUser> p = this.findPage(gson);
        List<DUser> dUsers = p.getContent();
        if(!CollectionUtils.isEmpty(dUsers)){
            dUsers.forEach(dUser -> {
                dUser.set("showUpdateTime", DateUtil.TimestampToString_1(dUser.getUpdateTime()));
                dUser.set("showUserSex", dUser.getUserSex()==1?"男":"女");
                dUser.set("showIsMarry", dUser.getIsMarry()==0?"是":"否");
            });
        }
        resultMap.put("total", p.getTotalElements());
        resultMap.put("rows", dUsers);
        resultMap.put("page",p.getNumber());
        return resultMap;
    }

    public Map add(DUser dUser,String orgCode){
        DUserOrg dUserOrg = new DUserOrg();
        dUserOrg.setUserAccount(dUser.getUserAccount());
        dUserOrg.setOrgCode(orgCode);
        dUserOrgService.save(dUserOrg);
        Integer flag = this.save(dUser);
        return ResultMap.build().add(flag>0).get();
    }

    public Map upd(DUser dUser,String orgCode){
        DUserOrg dUserOrg = dUserOrgService.findSingle(ParamBulider.build().addEqual("userAccount",dUser.getUserAccount()).get());
        if(dUserOrg!=null){
            dUserOrgService.delete(ParamBulider.build().addEqual("userAccount",dUser.getUserAccount()).get());
        }
        DUserOrg duo = new DUserOrg();
        duo.setOrgCode(orgCode);
        duo.setUserAccount(dUser.getUserAccount());
        dUserOrgService.save(duo);

        Integer flag = this.save(dUser);
        return ResultMap.build().update(flag>0).get();
    }

    public Map del(List<Integer> ids){
        List<DUser> dUsers = this.find(ParamBulider.build().add("id_in", ListUtil.ListToString(ids,",")).get());
        dUsers.forEach(dUser -> {
            dUser.setIsDelete(1);
        });
        return ResultMap.build().delete(this.save(dUsers)).get();
    }

    public Map lock(Integer id){
        DUser dUser = this.get(id);
        dUser.setIsLock(dUser.getIsLock()==0?1:0);
        this.save(dUser);
        return ResultMap.build().successState().successCode().message(dUser.getIsLock()==0?"解锁成功":"锁定成功").get();
    }

}
